<template>
    <div>
        <el-card>
            <!-- <el-button @click="addProductDilaog"></el-button> -->
            <el-table :data="postList" style="width: 100%">
                <el-table-column label="ID" prop="id"> </el-table-column>
                <el-table-column label="名称" prop="title"> </el-table-column>
                <el-table-column label="内容" prop="content"> </el-table-column>
                <el-table-column label="发帖人">
                    <template slot-scope="props">{{props.row.user.name}}</template>
                </el-table-column>
                <el-table-column label="发帖时间" prop="created_at"> </el-table-column>
                <el-table-column fixed="right" label="操作" width="180">
                    <template slot-scope="scope">
                        <el-button
                            @click="delPost(scope.row.id)"
                            type="danger"
                            size="small"
                        >
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination
                layout="prev, pager, next"
                @current-change="getlist"
                :total="this.total"
            >
            </el-pagination>
        </el-card>
    </div>
</template>

<script>
import { serviceRequest } from "network/request.js";
import { Message } from "element-ui";

export default {
    created() {
        this.getlist();
        // this.getCategory();
    },
    data() {
        return {
            postList: [],
            editId: "",
            dialogVisible: false,
            offset: 0,
            total: 0,
            categorys: [],
            categorysMap: {},
            productInfo: {
                title:  '',
                desc: '',
                condition: '',
                img: '',
                goods_category_id: '',
                id: '',
            },
            fileList: [],
        };
    },
    methods: {
        getlist(page) {
            if ('number' === typeof page) {
                this.offset  = 10 * (page - 1);
            } else {
                this.offset = 0;
            }
            const params = {
                url: '/posts/get',
                method: 'get',
                data: {
                    offset: this.offset,
                }
            }
            serviceRequest(params).then((res) => {
                this.postList = res.content.data;
                this.total = res.content.total;
            });
        },
        delPost(id) {
            this.$confirm(`删除后不可恢复，是否确定`, "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                const params = {
                    url: "/user/post/del",
                    method: "post",
                    data: {
                        ids: [id],
                    },
                };
                serviceRequest(params).then((res) => {
                    Message.success("删除成功! ");
                    this.getlist();
                });
            })
            .catch(() => {});
        },
    },
};
</script>

<style lang="scss" scoped>
</style>